/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Login;
import hibernate.entity.ResetRequest;

/**
 *
 * @author Neo
 */
public class ResetPasswordDAO extends AbstractDAO {

    LoginDAO lgnDao = new LoginDAO();

    public ResetRequest getByCodeAndEmail(String genCode, String email) {
        query = session.createQuery("from ResetRequest where genCode=:genCode and email = :email ");
        query.setParameter("genCode", genCode);
        query.setParameter("email", email);
        try {
            return (ResetRequest) query.list().get(0);
        } catch (Exception e) {
            return null;
        }
    }

    public int resetPassword(String email, String newPass) {
        try {
            Login lg = lgnDao.readByEmail(email);
            lg.setPassword(newPass);
            tx = session.beginTransaction();
            session.save(lg);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }
    }

    public int create(ResetRequest rq) {
        try {
          
            tx = session.beginTransaction();
            session.save(rq);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }
    }
}
